package utils
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.utils.setTimeout;
	
	/**
	 *  
	 * @author 青竹。
	 * 
	 */
	public class MouseWheelTrap {
		private static var _mouseWheelTrapped :Boolean;
		private static var _value:Boolean;
		private static var _stage:Stage;
		private static var _funs:Vector.<Function> = new Vector.<Function>();
		/**
		 * 注册一个接收方法，方法需要接收一个int型参数index。
		 *  index大0正向滚轮，小于0为反向滚轮。
		 * @param f
		 * 
		 */
		public static function regFun(f:Function):void{
			_funs.push(f);
		}
		/**
		 *  
		 * @param stage 舞台 
		 * @param useStageWheel 是否才用原生stage鼠标滚轮。
		 * 
		 */
		public static function action(stage:Stage,useStageWheel:Boolean=false):void{
			_stage=stage;
			if(!useStageWheel && ExternalInterface.available){
				ExternalInterface.addCallback("onFunc",onFunc);
				_stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse);
				_stage.addEventListener(FullScreenEvent.FULL_SCREEN,onFullScreen)
			}else{
				_stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			}
			if (ExternalInterface.available)
			{
				ExternalInterface.call("eval", code.toString());
			}else{
				trace("无可用的js接口!");
			}
		}
		private static function onFullScreen(e:FullScreenEvent):void{
			if(e.fullScreen){
				_stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			}else{
				_stage.removeEventListener(MouseEvent.MOUSE_WHEEL,onWheel);	
			}
		}
		private static function onWheel(e:MouseEvent):void{
			onFunc(e.delta);
		}
		
		/**
		 * 暂停。 
		 */
		public static var wheelStop:Boolean=true
		private static function onFunc(index:int):void{
			if(!wheelStop) return;
			for(var i:int=0;i<_funs.length;i++){
				_funs[i](index);
			}
		}
		private static function mouse(e:Event):void{
			 _stage.removeEventListener(e.type,mouse);
			 if(e.type==MouseEvent.MOUSE_MOVE){
			     _stage.addEventListener(Event.MOUSE_LEAVE,mouse)
			 }else{
			    _stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse);
			 }
			  allowBrowserScroll(e.type!=MouseEvent.MOUSE_MOVE)
		}
		
		private static function allowBrowserScroll(allow:Boolean):void
		{
			if(allow==_value){
				return;
			}
			
			if (ExternalInterface.available) 
			{
				ExternalInterface.call("allowBrowserScroll", allow);
			}
			_value=allow;
		}
		
		private static const code:XML=<code>
					<![CDATA[
							function thisMovie(movieName) {
								if (navigator.appName.indexOf('Microsoft') != -1) {
									return window[movieName];
								} else {
									return document[movieName];
								}
							}
							var browserScrolling;
							function allowBrowserScroll(value){
								browserScrolling=value;
							}
							
							function handle(delta){
								if(!browserScrolling){
									return false;
								}
								return true;
							}
							
							function wheel(event){
								var delta=0;
								if(!event){
									 event=window.event;
								}
								if(event.wheelDelta){
									  delta=event.wheelDelta/120;
									  if(window.opera){
										 delta=-delta;
									  }
								}else if(event.detail){
									delta=-event.detail/3;
								}
								if(delta){
									handle(delta);
								}
								if(!browserScrolling){
									thisMovie('GameLoader').onFunc(delta);
									if(event.preventDefault){
										event.preventDefault();
									}
									event.returnValue=false;
								}
							}
							
							if(window.addEventListener)
							{
								window.addEventListener('DOMMouseScroll',wheel,false);
							}
							window.onmousewheel=document.onmousewheel=wheel;
						]]>
					   </code>
	}
	
}
